package com.zac.coroutine.ui

import android.app.Activity
import android.content.Intent
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import android.widget.Toast
import com.zac.coroutine.R
import com.zac.coroutine.ext.log
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.cancel
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.supervisorScope
import java.lang.Exception

class MainActivity : Activity(), CoroutineScope by CoroutineScope(Dispatchers.IO) {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        main()
//        second()
//        third()
//        four()
//        five()
//        doIt()


        setContentView(R.layout.activity_main)

        findViewById<TextView>(R.id.tv).setOnClickListener {
            Toast.makeText(this, "点击了A ,五秒后执行跳转", Toast.LENGTH_SHORT).show()
            it.postDelayed(
                {
                    val intent = Intent(this, SecondActivity::class.java)
                    intent.addFlags(FLAG_ACTIVITY_NEW_TASK)

                    startActivity(intent)
                }, 5000
            )
        }

    }

    fun doIt() {
        launch {
            fetchTwoDocs()
        }
    }

    fun main() {
        GlobalScope.launch(context = Dispatchers.IO) {
            delay(600)
            log("launch A")
        }
        runBlocking {
            launch {
                log("launch")
                delay(500)
                log("launch after")
            }
        }

        GlobalScope.launch(context = Dispatchers.IO) {
            delay(600)
            log("launch B")
        }
        /*  GlobalScope.launch {
              launch {
                  delay(400)
                  log("launch A")
              }
              launch {
                  delay(300)
                  log("launch B")
              }
              log("end")
          }*/
        log("end")
    }

    fun second() = runBlocking {
        launch {
            delay(100)
            log("Task form runBlocking")
        }
        coroutineScope {
            launch {
                delay(500)
                log("Task from nested launch")
            }
            delay(50)
            log("Task from coroutine scope")
        }
        log("coroutine scope is over")
    }

    fun third() = runBlocking {
        launch {
            delay(100)
            log("Task from runBlocking")
        }
        supervisorScope {
            launch {
                delay(500)
                log("Task throw Exception")
                throw Exception("ex")
            }
            launch {
                delay(600)
                log("Task from nested launch")
            }
            log("coroutine scope is over")
        }
    }

    fun four() {
        mainScope.launch {
            repeat(5) {
                delay(1000L * it)
                log("重复第$it 次")
            }
        }
    }

    fun five() = runBlocking {
        val a = async {
            delay(4000)
            2
        }
        val b = async {
            delay(4000)
            4
        }

        log("结果:${a.await()}${b.await()}")

    }

    suspend fun fetchTwoDocs() = coroutineScope {
        val first = async {
            delay(1000)
            log("执行完了1")
        }
        val second = async {
            delay(5000)
            log("执行完了2")
        }

        listOf(first, second).awaitAll()
    }


    private val mainScope = MainScope()

    override fun onDestroy() {
        super.onDestroy()
        mainScope.cancel()
    }


}
